//
//  SCBaseTableViewController.m
//  Shuaiche
//
//  Created by dangshuai on 2018/12/18.
//  Copyright © 2018 zzs. All rights reserved.
//

#import "SCBaseTableViewController.h"
#import <MJRefresh.h>
#import "SCNetworkStatusConst.h"
#import "SCPlacehodlerView.h"
@interface SCBaseTableViewController ()
@property (nonatomic, strong) SCPlacehodlerView *placehoderView;
@end

@implementation SCBaseTableViewController {
    UILabel *_emptyTipLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _pageNo = 1;
    _pageSize = 20;
    [self createTableView];
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }
}

- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:[self tableViewFrame] style:[self tableViewStyle]];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0);
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorColor = SC_Color_Separator;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 15.0, *)) {
        _tableView.sectionHeaderTopPadding = 0;
    }
}

- (void)reloadTableView {
    [_tableView reloadData];
    _noDataView.hidden = YES;
    if (_dataSource.count == 0) {
        self.noDataView.hidden = NO;
        _tableView.tableHeaderView.hidden = YES;
    } else {
        _tableView.tableHeaderView.hidden = NO;
    }
}

#pragma mark -- TableView Create

- (UITableViewStyle)tableViewStyle {
    return UITableViewStylePlain;
}

- (CGRect)tableViewFrame {
  return CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT-kSCNavigationHeight);
//    return [UIScreen mainScreen].bounds;
}

- (UIEdgeInsets)tableViewContentInsets {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

#pragma mark -- Reload

- (void)addGifHeaderRefresh {
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRequestData)];
    [header setImages:self.refreshImages forState:MJRefreshStateRefreshing];
    [header setImages:self.refreshImages forState:MJRefreshStateIdle];
    [header setImages:self.refreshImages forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
}

- (void)addGifHeaderAutoRefresh:(BOOL)refresh {
    [self addGifHeaderRefresh];
    if (refresh) {
        [self autoRefresh];
    }
}

- (void)removeHeaderRefrehs {
    self.tableView.mj_header = nil;
}

- (NSMutableArray *)normalImages {
    if (_normalImages == nil) {
        _normalImages = [[NSMutableArray alloc] init];
        for (NSUInteger i = 1; i<=10; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_%@", @(i)]];
            [self.normalImages addObject:image];
        }
    }
    return _normalImages;
}

//正在刷新状态下的图片
- (NSMutableArray *)refreshImages {
    if (_refreshImages == nil) {
        _refreshImages = [[NSMutableArray alloc] init];
        for (NSUInteger i = 1; i<=10; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_%@", @(i)]];
            [self.refreshImages addObject:image];
        }
    }
    return _refreshImages;
}

- (void)addFooterRefresh {
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.stateLabel.textColor = SCColor(0x999999);
    [footer setTitle:@"" forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    self.tableView.mj_footer = footer;
}

- (void)headerRequestData{
    [self.tableView.mj_footer resetNoMoreData];
    [self pullRequestData];
}

- (void)autoRefresh {
    [self.tableView.mj_header beginRefreshing];
}

- (void)pullRequestData{}
- (void)loadMoreData{}

- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)endRefreshWithNoMoreData:(BOOL)noMore {
    [self.tableView.mj_header endRefreshing];
    if (noMore) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        [self.tableView.mj_footer setHidden:NO];
    } else {
        [self.tableView.mj_footer endRefreshing];
    }
}

- (void)noMoreData {
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_footer setHidden:NO];
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (NSMutableDictionary *)params {
    if (!_params) {
        _params = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _params;
}

- (UIView *)noDataView {
    if (!_noDataView) {
        
        if (![self emptyImageName]) {
            return nil;
        }
        
        _noDataView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, CGRectGetHeight(self.tableView.bounds) - self.tableView.contentInset.top)];
        _noDataView.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundView = _noDataView;
        
        SCPlacehodlerView *view = [[SCPlacehodlerView alloc] init];
        [_noDataView addSubview: view];
        view.imageName = [self emptyImageName];
        view.title = [self emptyTitleString];
        view.message = [self emptyTipString];
        view.buttonTitle = [self emptyButtonTitle];
        view.eventInvocation = [self buttonInvocation];
        _placehoderView = view;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@15);
            make.right.equalTo(@(-15));
            make.centerY.equalTo(_noDataView.mas_centerY).offset(-kSCNavigationHeight/2);
        }];
    }
    return _noDataView;
}

- (void)updateEmptyTipString:(NSString * _Nullable)tip {
    _placehoderView.message = tip;
}

- (void)updateEmptyButtonTitle:(NSString * _Nullable)title {
    _placehoderView.buttonTitle = title;
    if (title) {
        _placehoderView.eventInvocation = [self buttonInvocation];
    }
}

- (NSString *)emptyImageName {
    return @"emp_group";
//    return @"icon_load_carDetail_failed";
}

- (NSString *)emptyTitleString {
    return nil;
}

- (NSString *)emptyButtonTitle {
    return nil;
}

- (NSInvocation *)buttonInvocation {
    if (self.buttonEventName) {
        SEL sel = NSSelectorFromString(self.buttonEventName);
        NSMethodSignature *signature = [self methodSignatureForSelector:sel];
        if (signature) {
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
            invocation.target = self;
            invocation.selector = sel;
            return invocation;
        }
    }
    return nil;
}

- (NSString *)emptyTipString {
    return @"暂无数据";
}

- (void)registerCellWithNib:(NSString *)cellName {
    [self.tableView registerNib:[UINib nibWithNibName:cellName bundle:nil] forCellReuseIdentifier:cellName];
}

- (void)registerCellClass:(Class)cls {
    [self.tableView registerClass:cls forCellReuseIdentifier:NSStringFromClass(cls)];
}

@end
